package nextEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type CorpEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	CompanyName string // 公司名称
	CompanyType string // 公司名称

	Province string // 省
	City     string // 市
	Industry string // 行业

	Data map[string]interface{} // 配置参数
}

type corpPo struct {
	Id         int64 `json:"id" db:"id"`                  // 对象id
	CreateTime int64 `json:"createTime" db:"create_time"` // 创建时间
	UpdateTime int64 `json:"updateTime" db:"update_time"` // 更新时间

	CompanyName string `json:"companyName" db:"company_name"` // 名称
	CompanyType string `json:"companyType" db:"company_type"` // 类型

	Province string `json:"province" db:"province"`
	City     string `json:"city" db:"city"`
	Industry string `json:"industry" db:"industry"`

	Data map[string]interface{} `json:"corpData" db:"corp_data"` // 数据
}

func (e *CorpEntity) Type() string {
	return "CorpEntity"
}

func (e *CorpEntity) TableName() string {
	return "tb_corp"
}

func (e *CorpEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *CorpEntity) Instance() *CorpEntity {
	return &CorpEntity{
		BaseEntity: new(commEntity.BaseEntity),
		Data:       make(map[string]interface{}),
	}
}

func (e *CorpEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *CorpEntity) NewPo() interface{} {
	return &corpPo{
		Data: make(map[string]interface{}),
	}
}

func (e *CorpEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *CorpEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.CompanyName)
	return p
}

func (e *CorpEntity) BuildByJson(jsn *string) error {
	var po corpPo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *CorpEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *CorpEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*corpPo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime

	ptr.Data = po.Data
	ptr.CompanyName = po.CompanyName
	ptr.CompanyType = po.CompanyType
	ptr.Province = po.Province
	ptr.City = po.City
	ptr.Industry = po.Industry
}

func (e *CorpEntity) BuildToPo() interface{} {
	ptr := e
	po := corpPo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime

	po.Data = ptr.Data
	po.CompanyName = ptr.CompanyName
	po.CompanyType = ptr.CompanyType
	po.Province = ptr.Province
	po.City = ptr.City
	po.Industry = ptr.Industry

	return &po
}
